package cc.cc4414.cc02.chat.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import cc.cc4414.cc02.chat.entity.MessageSend;

/**
 * @author cc 2018年8月16日
 */
public interface MessageSendRepository extends JpaRepository<MessageSend, Long> {
	Integer countBySenderIdAndReceiverIdAndReceiverTypeAndIdGreaterThan(Long senderId, Long receiverId,
			Integer receiverType, Long id);

	Integer countByReceiverIdAndReceiverTypeAndIdGreaterThan(Long groupId, Integer receiverType,
			Long lastMessageSendId);

	/**
	 * 查询好友发送的消息
	 * 
	 * @param friendId
	 *            好友id
	 * @param userId
	 *            用户id
	 * @param start
	 *            从第几条开始
	 * @param size
	 *            查询的数量
	 * @return 聊天数据
	 */
	@Query(value = "select * from message_send where receiver_type = 1 and ((receiver_id = ?2 and sender_id = ?1) or (receiver_id = ?1 and sender_id = ?2)) order by id desc limit ?3, ?4", nativeQuery = true)
	List<MessageSend> findFriendMessageSend(Long friendId, Long userId, int start, int size);

	/**
	 * 查询群消息
	 * 
	 * @param groupId
	 *            群id
	 * @param start
	 *            从第几条开始
	 * @param size
	 *            查询的数量
	 * @return 聊天数据
	 */
	@Query(value = "select * from message_send where receiver_type = 2 and receiver_id = ?1 order by id desc limit ?2, ?3", nativeQuery = true)
	List<MessageSend> findGroupMessageSend(Long groupId, int start, int size);
}
